package com.example.systemwork.service;

import com.example.systemwork.common.service.LogicService;
import com.example.systemwork.common.utils.Result;
import com.example.systemwork.dao.*;
import com.example.systemwork.entity.*;
import io.swagger.annotations.ApiParam;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CustomerService extends LogicService<CustomerDao,Customer,Long> {
    public CustomerService(@Autowired CustomerDao dao,
                           ShopItemDao shopItemDao,
                           CustomerDao customerDao) {
        super(dao);
        this.shopItemDao = shopItemDao;
        this.customerDao = customerDao;
    }
    @Resource
    private ShopDao shopDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private final ShopItemDao shopItemDao;
    @Resource
    private final CustomerDao customerDao;
    @Resource
    private LineItemDao lineItemDao;

    public Torder getOrder (Long userId , Long shopId ){
        Customer customer=getDAO().getReferenceById(userId);
        Shop shop=shopDao.getReferenceById(shopId);
        Torder torder=customer.createOrder(shop);
        orderDao.save(torder);
        return torder;
    }

    public Torder addShopItemToOrder (Long orderId ,Long shopItemId ,Double quantity){
        Torder torder=orderDao.getReferenceById(orderId);
        ShopItem shopItem=shopItemDao.getReferenceById(shopItemId);
        torder.addItem(shopItem.getItem(),quantity);
        orderDao.save(torder);
        return torder;
    }

    public Torder removeShopItemToOrder (long orderId ,long lineItemId ){
        Torder torder=orderDao.getReferenceById(orderId);
        LineItem lineItem2=lineItemDao.getReferenceById(lineItemId);
//        ShopItem shopItem=shopItemDao.getReferenceById(shopItemId);
        LineItem lineItem=torder.removeItem(lineItem2.getItem());
        if(lineItem!=null){
            lineItem.setIsDeleted(1);
            lineItemDao.save(lineItem);
        }
        orderDao.save(torder);
        return torder;
    }

    public boolean payOrder ( Long userId , Long orderId ) {
        Customer customer=customerDao.getReferenceById(userId);
        Torder torder=orderDao.getReferenceById(orderId);
        torder.setStatus(1);
        orderDao.save(torder);
        return true;
    }

    public boolean cancelOrder ( Long userId , Long orderId ) {
        Customer customer=customerDao.getReferenceById(userId);
        Torder torder=orderDao.getReferenceById(orderId);
        torder.setIsDeleted(1);
        orderDao.save(torder);
        return true;
    }

    public boolean confirmOrder ( Long userId , Long orderId ) {
        Customer customer=customerDao.getReferenceById(userId);
        Torder torder=orderDao.getReferenceById(orderId);
        torder.setStatus(2);
        orderDao.save(torder);
        return true;
    }

    public boolean refundOrder ( Long userId , Long orderId ) {
        Customer customer=customerDao.getReferenceById(userId);
        Torder torder=orderDao.getReferenceById(orderId);
        torder.setStatus(3);
        orderDao.save(torder);
        return true;
    }

}
